home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / programm.ing / sources.arc / TTT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-22  |  6.6 KB  |  282 lines

  1. /* Will spin a box in 3d space. 
  2.  
  3.    o ) User can controll x,y,z rotation by keys 1,2 or 3
  4.    o ) Perspective viewing 
  5.    o ) Movement of object in its Object-Coords relative to its origin
  6.        (Default is origin at center of object, so any rotation will
  7.         just make it 'spin' in the x,y,z direction...rather booring
  8.         So change its co-ords w/ respect to its origin to get cool
  9.         tumbling rotations)
  10.    o ) Movement of camera. You can move the camera in the x,y or z
  11.        direction. (ie. You can move to the left or right of the object
  12.                        or you can view it close up or far away )
  13.   
  14.    Jan. 8 ,1994  jeff bilger 
  15.  
  16. */
  17.  
  18. #include <math.h>
  19. #include <linea.h>
  20. #include <osbind.h>
  21. int pts[4][2] = { 
  22.     320, 050,
  23.     120, 150,
  24.     520, 150,
  25.     320, 050
  26. };
  27. lineaport *theport;
  28.  
  29.  
  30. #define M1 65536L
  31. #define MF 65536.0
  32. #define BITSH 16
  33. #define PI 205887L
  34. #define SPININC (PI >> 4)
  35. #define addpt(p1,p2,p3) \
  36.         { p3.x = p1.x + p2.x; \
  37.           p3.y = p1.y + p2.y; \
  38.           p4.z = p1.z + p2.z; }
  39.  
  40. #define cosf(x) (cos((double) x / M1))
  41. #define sinf(x) (sin((double) x / M1))
  42.  
  43. typedef struct {double x,y,z;} point3d;
  44.  
  45. #define perx(p) \
  46.         (perspective ? ((p.x/(p.z+pdist))*pdist):p.x)
  47. #define pery(p) \
  48.         (perspective ? ((p.y/(p.z+pdist))*pdist):p.y)
  49.  
  50.  
  51.  
  52. #define NPTS 64
  53. #define NLINES 64
  54. #define NFACES 64
  55. #define I 100.0
  56.  
  57.  
  58. point3d point_[NPTS] =
  59.         { -10.0,-10.0,10.0,   10.0,-10.0,10.0,  10.0,10.0,10.0,  -10.0,10.0,10.0,
  60.            -10.0,-10.0,-10.0, 10.0,-10.0,-10.0, 10.0,10.0,-10.0, -10.0,10.0,-10.0 },
  61.         drawpt1[NPTS],drawpt2[NPTS];
  62. long spininc = SPININC >> 2;
  63.  
  64. int drawclr,
  65.     white,
  66.     glasses = 0,
  67.     perspective = 1,
  68.     npts  = 8,
  69.     linefrom[NLINES] = {0,1,2,3,0,4,7,1,5,6,6,5},
  70.     lineto[NLINES]   = {1,2,3,0,4,7,3,5,6,2,7,4},
  71.     from[NLINES]     = {0,1,1,5,4,7,0,3,2,6,1,0},
  72.     to[NLINES]       = {1,2,5,6,7,6,3,7,6,7,0,4},
  73.     nlines = 12;
  74.  
  75. double pdist=200.0;
  76.  
  77. unsigned int pause = 8092;
  78.  
  79. int x_offset=320,y_offset=100,z_offset=1;
  80.  
  81.  
  82. main()
  83. {
  84. register int i,j;
  85. point3d pointi;
  86. int color =1;
  87. double cos_2,sin_2;
  88. char com;
  89.  
  90. cos_2 = cos( (double) (6.0*3.141596/180.0));  /* precompute values */
  91. sin_2 = sin( (double) (6.0*3.141596/180.0));
  92.  
  93. printf(" 1-Spin Y  2-Spin X  3-Spin Z\n");
  94. printf(" 4-Perspective ON/OFF\n");
  95. printf(" Move object (x,y,z) -> 7,8,9\n");
  96. printf(" Move view   x/X,y/Y,z/Z\n");
  97. theport = a_init();     
  98.    
  99.  
  100.     theport -> plane0 = 1;
  101.     theport -> plane1 = 0;
  102.     theport -> plane2 = 0;
  103.     theport -> plane3 = 0;
  104.  
  105.  
  106.  for(i=0;i<npts;i++)
  107.    {
  108.    pointi = point_[i];
  109.    point_[i].y =( pointi.y * cos_2  +
  110.                 pointi.z * sin_2);
  111.    point_[i].z =( pointi.y * -sin_2 +
  112.                 pointi.z * cos_2);
  113.    }
  114.  
  115.  
  116. for(i=0;i<nlines;i++)  /* set up points-to-connect lookup table*/
  117. { drawpt1[i] = point_[linefrom[i]];
  118.   drawpt2[i] = point_[lineto[i]];  
  119. }
  120.  
  121.  
  122.  
  123. while(1)
  124. {
  125.  com = Bconin(2);
  126.  
  127.  for(i=0;i<npts;i++)
  128.    {
  129.    pointi = point_[i];
  130.    
  131.     if(com == 0x31) {              /* spin y */
  132.                point_[i].x =( pointi.x * cos_2  -
  133.                               pointi.z * sin_2);
  134.                point_[i].z =( pointi.x * sin_2 +
  135.                               pointi.z * cos_2);
  136.                     }
  137.     if(com == 0x32) {              /* spin x */
  138.                 point_[i].y =( pointi.y * cos_2  +
  139.                                pointi.z * sin_2);
  140.                 point_[i].z =( pointi.y * -sin_2 +
  141.                                pointi.z * cos_2);
  142.                     }
  143.     if(com == 0x33) {              /* spin z */
  144.                 point_[i].x =( pointi.x * cos_2  +
  145.                                pointi.y * sin_2);
  146.                 point_[i].y =( pointi.y * cos_2  -
  147.                                pointi.x * sin_2);
  148.                     }
  149.     if(com == 0x37) point_[i].x++;      /* These will move the */
  150.     if(com == 0x38) point_[i].y++;      /* object around in its */
  151.     if(com == 0x39) point_[i].z++;      /* OWN co-ord sys!! Thus */
  152.                                         /* changing these will effect*/
  153.                                         /* how it rotates about the origin */
  154.                                         /* The default is that the origin*/
  155.                                         /* is at the CENTER of the object */
  156.                         /* remember ** all rotations are about the origin, so if your object
  157.                            is AT the origin it spins in place, if it's not at the origin, 
  158.                            it spins AROUND the origin */
  159.  
  160.    }
  161.  
  162. if( (com > 0x77 && com < 0x7b) || ( com > 0x57 && com < 0x5b) ) 
  163.  {
  164.     for(i=0;i<nlines;i++) draw3dline(drawpt1[i],drawpt2[i],0);
  165.     if(com == 0x78) x_offset++;
  166.     if(com == 0x79) y_offset++;
  167.     if(com == 0x7a) z_offset++;
  168.     if(com == 0x58) x_offset--;
  169.     if(com == 0x59) y_offset--;
  170.     if(com == 0x5a) z_offset--;
  171.    com = 0x36; /* so that code below will execute */
  172.  } 
  173.  
  174. if(com < 0x37)          /* if 1,2,3 chosen, then display it */
  175.   for(i=0;i<nlines;i++)
  176.    { draw3dline(drawpt1[i],drawpt2[i],0);  /*erase */
  177.      draw3dline(drawpt1[i]=point_[linefrom[i]],drawpt2[i]=point_[lineto[i]],color);
  178.      /* draw it */
  179.   }
  180.    
  181. /*
  182. compute_normals();
  183. */
  184.  
  185. if(com == 0x34) perspective = perspective ^ 1;
  186. }
  187.  
  188. }
  189. draw3dline(p1,p2,color)
  190. point3d p1,p2;
  191. int color;
  192. {
  193.  int x1,y1,x2,y2;
  194.  
  195.  x1 = (int)( perx(p1) ) +x_offset;
  196.  y1 = (int)( pery(p1) ) +y_offset;
  197.  x2 = (int)( perx(p2) ) +x_offset;
  198.  y2 = (int)( pery(p2) ) +y_offset;
  199.  
  200.  
  201. theport -> plane0 = color;
  202.  
  203.  
  204. /*
  205. printf(" %d,%d  %d,%d\n",x1,y1,x2,y2);
  206. */
  207.  
  208.  
  209. a_line(x1,y1,x2,y2); 
  210.  
  211.  
  212. /* map second cube */
  213.  
  214.  x1-= 50;
  215.  y1+= 20;
  216.  x2-= 50;
  217.  y2+= 20;
  218.  
  219. a_line(x1,y1,x2,y2); 
  220.  
  221.  
  222. /* map 3rd cube */
  223.  x1+=100; 
  224.  x2+=100;
  225.  
  226. a_line(x1,y1,x2,y2); 
  227.  
  228.  
  229.  
  230.  
  231. }
  232.  
  233. mu(a,b)
  234. register long int a,b;
  235. {
  236.  asm
  237.     {
  238.      move.l a,D0
  239.      move.l b,D1
  240.      muls   D0,D1
  241.     }
  242. }
  243. di(a,b)
  244. register long int a,b;
  245. {
  246.  asm
  247.     {
  248.      move.l a,D0
  249.      move.l b,D1
  250.     }
  251. }
  252.  
  253.  
  254.  
  255. compute_normals()
  256. {
  257.  point3d A,B;
  258.  int h;
  259.   double An,Bn,ycomp;
  260.  
  261.  for(h=0;h<6;h++)
  262.    {
  263.     A.x = point_[to[h]].x - point_[from[h]].x;
  264.     A.z = point_[to[h]].z - point_[from[h]].z;
  265.     A.y = point_[to[h]].y - point_[from[h]].y;
  266.     B.x = point_[to[h+1]].x - point_[from[h+1]].x;
  267.     B.z = point_[to[h+1]].z - point_[from[h+1]].z;
  268.     B.y = point_[to[h+1]].y - point_[from[h+1]].y;
  269.  
  270.     An = sqrt(A.x*A.x+A.z*A.z+A.y*A.y);
  271.     Bn = sqrt(B.x*B.x+B.z*B.z+B.y*B.y);
  272.      A.x = A.x / An;
  273.     A.z = A.z / An;
  274.     B.x = B.x / Bn;
  275.     B.z = B.z / Bn;
  276.    
  277.     ycomp = A.z*B.x - A.x*B.z;
  278.     
  279.    }
  280.  
  281. }
  282.